
import java.text.DecimalFormat;
import java.util.Date;
import java.util.Vector;
import java.util.Enumeration;

import java.util.Arrays;
import javax.swing.JOptionPane;
import java.util.*;
import java.math.*;
import java.io.*;

@SuppressWarnings("serial")
public class GeraEstados {
	
	//accao,fO, fS, fE, fN, UltimaMigalha, bestRatio, bestDir, ComeuMigalha, V, D
	
	public static int RVisaoFantasmas = 4;
	public static int RVisaoMigalhas = 4;
	
	
	public static void main(String args[]) {
		
		int[] acao = {0,90,180,270};
		
		int[] raiovisaofantasma = new int[RVisaoFantasmas];
		int[] raiovisaomigalha = new int[RVisaoMigalhas];

		for(int n = 0; n < RVisaoFantasmas;n++)
			raiovisaofantasma[n] = n;
		
		for(int n = 0; n < RVisaoMigalhas;n++)
			raiovisaomigalha[n] = n;
	
		
		int[] ultimamigalha = {0,1};
		int[] direcaomelhor = {-1,0,90,180,270};
		int[] comeu = {0,1};
		
		ArrayList<Double> valor = new ArrayList<Double>();
		
		for(double n=0; n <= RVisaoMigalhas;n++)
		{
			for(double k=0; k <= n;k++)
			{
				double temp = rnd( k / (n + 1),1);
				if(!valor.contains(temp))
					valor.add(temp);
				
				System.out.println(k + " / " + (n+1) + " = " + temp);
				
			}
			
		}
		
		
		long contador = 0;
		System.out.println("Racios: " + valor.size());
		
		FileOutputStream out;
		PrintStream p;
		
		try
		{
			out = new FileOutputStream("estados.txt");

			//Adicionar estados finais
			String s = "[0 0 0 0 0 0 0 0 0 1 0]";
			out.write(s.getBytes());
			out.write("\r\n".getBytes());
			s = "1000";
			out.write(s.getBytes());
			out.write("\r\n".getBytes());
			
			s = "[0 0 0 0 0 0 0 0 0 0 1]";
			out.write(s.getBytes());
			out.write("\r\n".getBytes());
			s = "1000";
			out.write(s.getBytes());
			out.write("\r\n".getBytes());
		
		for(int a : acao)
		{
			for (int r1 : raiovisaofantasma)
			{
				for (int r2 : raiovisaofantasma)
				{
					for (int r3 : raiovisaofantasma)
					{
						for (int r4 : raiovisaofantasma)
						{
							for (int m : raiovisaomigalha)
							{
								for(double rb : valor)
								{
									for(int dm : direcaomelhor)
									{
										for(int c : comeu)
										{	
											s = "[" + a + " " + r1+" "+r2+" "+r3+" "+r4+" "+m+" "+rb+" " +dm+" "+c+ " " + "0 0]";
											out.write(s.getBytes());
											out.write("\r\n".getBytes());
											s = "0";
											out.write(s.getBytes());
											out.write("\r\n".getBytes());
											out.flush();
											contador++;
										}
									}
								}
							}
						}	
					}	
				}
				System.out.print(".");
			}
		}

			out.close();
			System.out.println("Calculados " + (contador + 2) + " estados.");
			
		}
		catch(Exception e)
		{
			System.err.println ("Ocorreu um erro com o ficheiro.");

		}
		
	
	}
	
	 public static double rnd(double value, int decimalPlace)
	    {
	      double power_of_ten = 1;
	      // floating point arithmetic can be very tricky.
	      // that's why I introduce a "fudge factor"
	      double fudge_factor = 0.05;
	      while (decimalPlace-- > 0) {
	         power_of_ten *= 10.0d;
	         fudge_factor /= 10.0d;
	      }
	      return Math.round((value + fudge_factor)* power_of_ten)  / power_of_ten;
	    }
	
}
	
	